<template>
  <div class="page">
    <div class="nav-bar flex-row">
      <div class="logo">Mtime</div>
      <div class="nav-item " v-for="home in homeList " :key='home' :class="{actived:currentChoose == home}" @click="choosen(home)">{{home}}</div>
    </div>
    <div class="color-background"></div>
    <div class="search-box">
      <div class="search-content flex-row">
        <div class="all-button">全部</div>
        <div class="iconfont icon-xia"></div>
        <div class="split-v"></div>
        <input
          class="keyword"
          id="keyword"
          type="text"
          placeholder="搜索电影、影人、文章、用户、家族"
        />
        <div class="iconfont icon-search" @click="searchBykeyword"></div>
      </div>
    </div>
    <div class="page-content flex-column">
      <div class="content-menu flex-row">
        <div class="menu-item actived">影视</div>
        <div class="menu-item">影人</div>
        <div class="menu-item">内容</div>
        <div class="menu-item">用户</div>
        <div class="menu-item">家族</div>
      </div>
      <div class="selected-box page-box flex-row">
        <div class="box-label">已选择：</div>
        <div class="selected-items flex-row">
          <div class="selected-item"
            v-for="selectedType in selectedTypeList"
            :key="selectedType.typeId" >
            {{ selectedType.name }}
            <div class="iconfont icon-delete"
              @click="removeType(selectedType)"
            ></div>
          </div>
          <br />
          <div
            class="selected-item"
            v-for="selectedyear in selectyearList"
            :key="selectedyear"
          >
            {{ selectedyear }}
            <div
              class="iconfont icon-delete"
              @click="removeYear(selectedyear)"
            ></div>
          </div>
        </div>
      </div>
      <div class="split-line"></div>
      <div class="types-box page-box flex-row">
        <div class="box-label">类型：</div>
        <div class="type-items flex-row">
          <div
            class="type-item"
            v-for="item in typeList"
            :class="{ actived: item.name == currentType.name }"
            :key="item.typeId"
            @click="typeClick(item)"
          >
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="split-line"></div>
      <div class="years-box page-box flex-row">
        <div class="box-label">年代：</div>
        <select class="year-select" @change="yearSelect">
          <option
            class="year-item"
            v-for="yearItem in yearList"
            :key="yearItem.movieYear"
          >
            {{ yearItem.movieYear }}
          </option>
        </select>
      </div>
      <div class="split-line"></div>
      <div class="page-box total-text flex-row">
        <div class="box-label">相关影视：</div>
        <div class="number">{{totalCount}}个</div>
      </div>
      <div class="movie-box page-box flex-column">
        <div
          class="movie-item"
          v-for="(movie, index) in movieList"
          :key="movie.movieId"
        >
          <div class="movie-ratio-box">
            <div class="ratio-label">总评分</div>
            <div class="ratio-value">{{ movie.rating }}</div>
          </div>
          <div class="movie-info-box">
            <div class="movie-title">
              {{ movie.name }} ({{ movie.movieYear }}年)
            </div>
            <div class="movie-info">
              <div class="movie-image">
                <img :src="movie.bigImage" />
              </div>
              <div class="movie-detail">
                <div class="line-text">
                  {{ movie.length }}分钟 - 类型：{{ splitType(index) }}
                </div>
                <div class="line-text">导演： {{ movie.directorName }}</div>
                <div class="line-text">主演： {{ splitActor(index) }}</div>
                <div class="movie-button">
                  <router-link :to="{
                    path:'/movie-info',
                    query:{
                      id:movie.movieId
                    }
                    }">查看详情
                  </router-link>
                  </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="page-box flex-row" id="pagingBox">
        <div
          class="last-button"
          :class="{ actived: currentPage == 1 }"
          @click="prepageChange(currentPage)"
        >&lt;
        </div>
        <div
          class="number-button"
          v-for="page in pages"
          :class="{ actived: currentPage == page }"
          :key="page"
          @click="pageChange(page)"
        >
          {{ page }}
        </div>
        <div
          class="next-button"
          :class="{ actived: currentPage == totalPage }"
          @click="nextpageChange(currentPage)"
        >
          &gt;
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      typeList: [],
      yearList: [],
      currentType: {
        typeId: "",
        name: "",
      },
      currentYear: "",
      selectedTypeList: [],
      selectyearList: [],
      selectTypeIdList: [],
      movieList: [],
      currentPage: 1,
      totalPage: "",
      pages: [1, 2, 3, 4, 5, 6, "..."],
      totalCount: "",
      keyword: "",
	  homeList: ['首页','电影','原创','榜单','社区','视频'],
	  currentChoose: '首页'
    };
  },
  methods: {
    choosen(homeitem) {
		this.currentChoose = homeitem;
	
	},
    loadAllTypes() {
      this.$http
        .get("http://118.25.42.197:9930/api/movies/categories")
        .then((response) => {
          if (response.status == 200) {
            this.typeList = response.data;
          }
        })
        .catch((err) => {
          console.log("获取分类列表错误", err.message);
        });
    },
    loadAllYear() {
      this.$http
        .get("http://118.25.42.197:9930/api/movies/years")
        .then((response) => {
          if (response.status == 200) {
            this.yearList = response.data;
          }
        })
        .catch((err) => {
          console.log("获取年份错误", err.message);
        });
    },
    loadMovieList() {
      this.$http
        .get(
          `http://118.25.42.197:9930/api/movies/movie-list?pageNumber=${
            this.currentPage - 1
          }&pageSize=6`
        )
        .then((response) => {
          if (response.status == 200) {
            this.movieList = response.data.content;
            this.totalCount = response.data.totalElements;
            console.log(this.movieList);
            this.totalPage = response.data.totalPages;
            this.pages = this.pages.concat(this.totalPage);
          }
        })
        .catch((err) => {
          console.log("获取电影列表错误", err.message);
        });
    },
    typeClick(typeItem) {
      //将被点击的分类赋值给当前分类变量
      this.currentType = typeItem;
      //将点击的类型数据加入被选择列表中
      if (this.selectedTypeList.length == 0) {
        this.selectedTypeList.push(this.currentType);
        this.selectTypeIdList.push(this.currentType.typeId);
        this.loadMoviepageList();
      } else if (
        this.selectedTypeList.find((item) => this.currentType === item) !==typeItem
      ) {
        this.selectedTypeList.push(this.currentType);
        this.selectTypeIdList.push(this.currentType.typeId);
        this.loadMoviepageList();
      }
    },
    yearSelect(event) {
      this.currentYear = event.target.value;
      //将点击的类型数据加入被选择列表中
      if (this.selectyearList.length == 0) {
        this.selectyearList.push(this.currentYear);
        this.loadMoviepageList();
      } else if (
        this.selectyearList.find((item) => this.currentYear === item) !==
        event.target.value
      ) {
        this.selectyearList.push(this.currentYear);
        this.loadMoviepageList();
      }
      console.log(this.selectyearList);
    },
    removeType(typeItem) {
      this.currentType = typeItem;
      let index = this.selectedTypeList.findIndex((item) => item === typeItem);
      if (index != -1) {
        this.selectedTypeList.splice(index, 1);
        this.selectTypeIdList.splice(index, 1);
        this.loadMoviepageList();
      }
    },
    removeYear(typeyearItem) {
      this.currentType = typeyearItem;
      let index = this.selectyearList.findIndex(
        (item) => item === typeyearItem
      );
      if (index != -1) {
        this.selectyearList.splice(index, 1);
      }
    },
    splitType(index) {
      let splitTypeArray = this.movieList[index].typeName.split(",").join("/");
      return splitTypeArray;
    },
    splitActor(index) {
      let splitActorArray = this.movieList[index].actorsName.split(",");
      return `${splitActorArray[0]}-${splitActorArray[1]}`;
    },
    prepageChange(page) {
      this.currentPage = page;
      if (this.currentPage > 1 && this.currentPage <= this.totalPage) {
        this.currentPage = page - 1;
        this.loadMoviepageList();
      }
    },
    nextpageChange(page) {
      this.currentPage = page;
      if (this.currentPage == 1 || this.currentPage < this.totalPage) {
        this.currentPage = page + 1;
        this.loadMoviepageList();
      }
    },
    pageChange(page) {
      this.currentPage = page;
      if (this.currentPage != "...") {
        this.loadMoviepageList();
      }
    },
    loadPages() {
      this.currentPage = 1;
    },
    loadMoviepageList() {
      let url = "";
      if (
        this.selectTypeIdList.length == 0 &&
        this.selectyearList.length == 0 &&
        this.keyword.trim() == ""
      ) {
        url =
          "http://118.25.42.197:9930/api/movies/movie-list?pageNum=" +
          (this.currentPage - 1) +
          "&pageSize=6";
      } else if (
        this.selectTypeIdList.length != 0 &&
        this.selectyearList.length == 0
      ) {
        url =
          "http://118.25.42.197:9930/api/movies/movie-list?pageNum=" +
          (this.currentPage - 1) +
          "&pageSize=6" +
          "&categories=" +
          this.selectTypeIdList;
      } else if (this.selectyearList.length != 0) {
        url =
          "http://118.25.42.197:9930/api/movies/movie-list?pageNum=" +
          (this.currentPage - 1) +
          "&pageSize=6" +
          "&movie_year=" +
          this.selectyearList;
      } else if (
        this.selectTypeIdList.length != 0 &&
        this.selectyearList.length != 0
      ) {
        url =
          "http://118.25.42.197:9930/api/movies/movie-list?pageNum=" +
          (this.currentPage - 1) +
          "&pageSize=6" +
          "&categories=" +
          this.selectTypeIdList +
          "&movie_year=" +
          this.selectyearList;
      } else if (this.keyword.trim() != "") {
        url =
          "http://118.25.42.197:9930/api/movies/movie-list?pageNum=" +
          (this.currentPage - 1) +
          "&pageSize=6" +
          "&keyword=" +
          this.keyword;
      }
      console.log(url);
      this.$http
        .get(url)
        .then((response) => {
          console.log("影片分页列表数据:", response);
          this.movieList = response.data.content;
          this.totalCount = response.data.totalElements;
          this.totalPage = response.data.totalPages;
        })
        .catch((err) => {
          console.error("影片分页列表数据错误:", err);
        });
    },
    searchBykeyword() {
      this.keyword = document.getElementsByClassName("keyword")[0].value;
      console.log(this.keyword);
      this.loadMoviepageList();
    },
  },
  mounted() {
    this.loadAllTypes();
    this.loadAllYear();
    this.loadMovieList();
    this.loadPages();
  },
};
</script>
<style>
@import url("//at.alicdn.com/t/font_2863451_npexvzpdos.css");

.flex-row {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.flex-column {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

body {
  padding: 0;
  margin: 0;
}

.page {
  width: 100%;
  padding-top: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.nav-bar {
  width: 100%;
  background-color: #1e2736;
  height: 60px;
  color: rgba(200, 200, 200, 1);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
}

.nav-bar .logo {
  font-size: 200%;
  font-weight: bold;
  margin-left: 10%;
  margin-right: 60px;
  color: #fff;
}

.nav-item {
  width: 80px;
  position: relative;
  font-size: 16px;
  cursor: pointer;
}

.nav-item.actived {
  color: #fff;
  font-weight: 500;
}

.nav-item.actived:after {
  content: "";
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-bottom-color: #fff;
  position: absolute;
  bottom: -20px;
  left: 8px;
}

.color-background {
  position: absolute;
  top: 60px;
  left: 0;
  width: 100%;
  background-color: #127bab;
  height: 300px;
}

.page-content {
  width: 80%;
  background-color: #fff;
  overflow-y: auto;
  z-index: 5;
  margin-top: 100px;
  position: relative;
  padding-top: 60px;
}

.search-box {
  width: 50%;
  height: 60px;
  margin: 0 auto;
  background-color: #127bab;
  padding: 10px;
  position: absolute;
  top: 120px;
  left: 25%;
  border-radius: 40px;
  z-index: 8;
  display: flex;
  justify-content: center;
  align-items: center;
}

.search-box .search-content {
  width: calc(100% - 10px);
  height: 50px;
  border-radius: 25px;
  background-color: #fff;
}

.search-box .search-content .all-button {
  margin-left: 20px;
  width: 60px;
  text-align: center;
  color: #33334d;
}

.search-box .search-content .icon-xia {
  margin-right: 20px;
}

.search-box .search-content .split-v {
  height: 30%;
  border-left: 1px solid rgba(200, 200, 200, 1);
  margin-right: 20px;
}

.search-box .search-content .keyword {
  height: calc(100% - 2px);
  border: none;
  flex: 1;
}

.keyword:focus {
  outline: none;
}

.search-box .search-content .icon-search {
  width: 40px;
  font-size: 150%;
  cursor: pointer;
}

.content-menu {
  width: calc(100% - 100px);
  padding: 0 50px;
  border-bottom: 1px solid rgba(200, 200, 200, 0.9);
  margin-bottom: 20px;
}

.content-menu .menu-item {
  padding: 10px 20px;
  margin-left: 10px;
  margin-right: 10px;
}

.content-menu .menu-item.actived {
  border-bottom: 2px solid #df4706;
}

.page-box {
  margin-left: 50px;
  margin-right: 50px;
  padding: 20px 0;
  border-bottom: 1px dotted rgba(200, 200, 200, 0.5);
  align-items: flex-start;
}

.page-box .box-label {
  width: 100px;
  flex-shrink: 0;
  color: #333;
}

.selected-item {
  border: 1px solid #0087e2;
  color: #0087e2;
  padding: 5px 10px;
  background-color: #ecf5ff;
  margin-right: 15px;
  border-radius: 4px;
  position: relative;
  padding-right: 30px;
}

.selected-item .icon-delete {
  width: 20px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  top: calc((100% - 20px) / 2);
  right: 5px;
  color: #0087e2;
  cursor: pointer;
}

.type-items {
  flex-wrap: wrap;
  align-items: flex-start;
}

.type-items .type-item {
  margin-right: 10px;
  margin: 0px 25px 25px 0px;
  color: #4e5e73;
}

.type-items .type-item.actived {
  color: #20a0da;
}

.years-box {
  align-items: center;
}

.year-select {
  width: 200px;
  padding: 0 10px;
  height: 40px;
  border-radius: 4px;
  border: 1px solid rgba(200, 200, 200, 0.5);
  cursor: pointer;
}

.year-select:focus {
  outline: none;
}

.movie-item {
  padding-left: 90px;
  position: relative;
  zoom: 1;
  margin-bottom: 40px;
}

.movie-item .movie-ratio-box {
  position: absolute;
  left: 20px;
  top: 33px;
  width: 55px;
  height: 55px;
  text-align: center;
  background: #679c21;
  color: #fff;
}

.movie-item .movie-ratio-box .ratio-label {
  padding: 4px 0 0;
  font-weight: 400;
  font-size: 12px;
  display: inline-block;
}

.movie-item .movie-ratio-box .ratio-value {
  font-size: 23px;
  font-weight: 700;
  position: absolute;
  left: 10px;
  top: 23px;
  text-align: center;
}

.movie-image {
  overflow: hidden;
  display: block;
  float: left;
  margin-top: -15px;
}

.movie-image img {
  width: 98px;
  height: 130px;
  vertical-align: top;
  border: 1px solid #c4c4c4;
  cursor: pointer;
}

.movie-detail {
  float: left;
  width: 450px;
  margin-top: -22px;
  margin-left: 10px;
  font-size: 13px;
  line-height: 10px;
}

.line-text {
  margin: 18px 0;
}

.movie-title {
  font-size: 20px;
  line-height: 24px;
  font-weight: 400;
  padding: 30px 0;
  background: #fff;
}

.movie-button {
  background: #679c21;
  border-radius: 18px;
  color: #fff;
  width: 124px;
  height: 28px;
  font-size: 14px;
  line-height: 28px;
  overflow: hidden;
  text-align: center;
  float: left;
  letter-spacing: 2px;
  margin-top: 5px;
  margin-left: 5px;
}

#pagingBox {
  text-align: center;
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
  display: flex;
  justify-content: center;
  margin-bottom: 25px;
}

.last-button {
  padding: 0 4px;
  font-size: 20px;
  min-width: 35.5px;
  height: 28px;
  line-height: 28px;
  box-sizing: border-box;
  text-align: center;
  background-color: #f4f4f5;
  cursor: pointer;
}

.last-button.actived {
  cursor: not-allowed;
  color: #c0c4cc;
}

.next-button.actived {
  cursor: not-allowed;
  color: #c0c4cc;
}

.number-button {
  padding: 0 4px;
  font-size: 13px;
  min-width: 35.5px;
  height: 28px;
  line-height: 28px;
  box-sizing: border-box;
  text-align: center;
  background-color: #f4f4f5;
  margin-left: 15px;
  border-radius: 4px;
  cursor: pointer;
}

.number-button.actived {
  background-color: #409eff;
  color: #fff;
}

.next-button {
  padding: 0 4px;
  font-size: 20px;
  min-width: 35.5px;
  height: 28px;
  line-height: 28px;
  box-sizing: border-box;
  text-align: center;
  background-color: #f4f4f5;
  margin-left: 15px;
}
</style>

